/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wallythewatermelon;

import java.awt.Graphics;

/**
 *
 * @author matthewweidman
 */
public class Watermelon extends Sprite {
    public static int wallySpeed = 16;
    public static int wallyJump = 16;
    public char sideHitting='0';
    public int blockMessageReceiver=-1;
    public int lastx, lasty;
    
    public Watermelon(int x, int y) 
    {
        super(x, y-20, "wally");
    }
    int blockX, blockY;
    
    public void calcBlockCoords()
    {
        int i, j;
        
        for(i=0;i<Level.LEVEL_WIDTH;i++)
        {
            for(j=0;j<Level.LEVEL_HEIGHT;j++)
            {
                if((this.x>=64*i&&this.x<=(64*i+1))||(this.y>=64*j&&this.y<=(64*j+1)))
                {
                    blockX=i;
                    blockY=j;
                }
            }
        }
        
        System.out.printf("Output bollocks=%d %d\n", blockX, blockY);
    }
    
    @Override
    public void draw(Graphics g) {
        lastx = this.x;
        lasty = this.y;
        super.draw(g);
        
        if (this.blockMessageReceiver == DrawManager.levelArray[DrawManager.getLevelIndex()].bcount) {
            sideHitting='0';
        }
        blockMessageReceiver = -1;
        
        if (KeyDetector.goingRight && this.x <= WallyTheWatermelon.WINDOW_WIDTH-this.width) {
            this.dx = wallySpeed;
        } else if (KeyDetector.goingLeft && this.x >= 0) {
            this.dx = -wallySpeed;
        } else {
            this.dx = 0;
        }
        
        System.out.println("dy = " + dy);
        System.out.println("ddy = " + ddy);
        
        if (standingOnBlock()) {
            System.out.println("Standing");
            this.ddy = 0;
            this.dy = 0;
        }
        else
        {
            this.ddy=1;
        }
        
        while(checkCollisionsBottom())
        {
            System.out.println("Colliding with bottom.");
            this.ddy=0;
            this.dy=0;
            y--;
        }
        
        while(checkCollisionsTop())
        {
            System.out.println("Colliding with top.");
            this.ddy=0;
            this.dy=0;
            y++;
        }
        
        if(checkCollisionsLeft())
        {
            this.dx=-1;
            this.ddy=0;
            x--;
        }
        
        if(checkCollisionsRight())
        {
            this.dx=1;
            this.ddy=0;
            x++;
        }
        
        if(levelShouldEnd())
        {
            System.out.println("Level should end.");
            DrawManager.loadNextLevel();
        }
    }
    
    public boolean levelShouldEnd()
    {
        Sprite endPointSprite=DrawManager.levelArray[DrawManager.getLevelIndex()].endPoint;
        //return ((wallyx>=(this.x-1) && wallyx<=(this.x+this.width+1)) && (wallyy>=(this.y-1)&&wallyy<=(this.y+this.width+1)));
        //return this.isCollidingWith(DrawManager.levelArray[DrawManager.getLevelIndex()].endPoint);
        return ((x>=(endPointSprite.x-1) && x<=(endPointSprite.x+endPointSprite.width+1)) && (y>=(endPointSprite.y-1)&&y<=(endPointSprite.y+endPointSprite.width+1)));
    }
    
    public boolean checkCollisionsLeft()
    {
        return sideHitting=='l';
    }
    
    public boolean checkCollisionsRight()
    {
        return sideHitting=='r';
    }
    
    //This is like the eject mechanism in SMB on the NES. It will be used to
    //determine if Wally is currently stuck in a block.
    
    public boolean checkBottom(Block in)
    {
        int i;
        int xPos;
        int yPos;
        
        int refLeftX, refLeftY;
        int refRightX;
        
        refLeftX=in.x+10;
        refLeftY=in.y;
        refRightX=in.x+in.width-10;
        
        yPos=y+height;
        
        for(i=0;i<this.width;i++)
        {
            xPos=this.x+i;
            
            if(xPos>=refLeftX&&xPos<=refRightX&&yPos>=refLeftY&&yPos<=refLeftY+in.height/2-1)
            {
                return true;
            }
        }
        
        return false;
    }
    
    public boolean checkTop(Block in)
    {
        int i;
        int xPos;
        int yPos;
        
        int refLeftX, refLeftY;
        int refRightX;
        
        refLeftX=in.x+10;
        refLeftY=in.y;
        refRightX=in.x+in.width-10;
        
        yPos=y;
        
        for(i=0;i<this.width;i++)
        {
            xPos=this.x+i;
            
            if(xPos>=refLeftX&&xPos<=refRightX&&yPos>=refLeftY&&yPos<=refLeftY+(in.height/2+in.height/4+in.height/8)-1)
            {
                return true;
            }
        }
        
        return false;
    }
    
    public boolean checkCollisionsBottom()
    {
        int i,j;
        Block blockFromSprite;
        
        for(i=0;i<Level.LEVEL_HEIGHT;i++)
        {
            for(j=0;j<Level.LEVEL_WIDTH;j++)
            {
                blockFromSprite=DrawManager.levelArray[DrawManager.getLevelIndex()].blocksArray[j][i];//new Block(DrawManager.levelArray[DrawManager.getLevelIndex()].levelData[j][i].x, DrawManager.levelArray[DrawManager.getLevelIndex()].levelData[j][i].y, DrawManager.levelArray[DrawManager.getLevelIndex()].levelData[j][i].img);
                //blockFromSprite.blockType=DrawManager.levelArray[DrawManager.getLevelIndex()].levelData[j][i].blockType;
                if (blockFromSprite!=null&&checkBottom(blockFromSprite))
                {
                    return true;
                }
            }
        }
        
        return false;
    }
    
    public boolean checkCollisionsTop()
    {
        int i,j;
        Block blockFromSprite;
        
        for(i=0;i<Level.LEVEL_HEIGHT;i++)
        {
            for(j=0;j<Level.LEVEL_WIDTH;j++)
            {
                blockFromSprite=DrawManager.levelArray[DrawManager.getLevelIndex()].blocksArray[j][i];//new Block(DrawManager.levelArray[DrawManager.getLevelIndex()].levelData[j][i].x, DrawManager.levelArray[DrawManager.getLevelIndex()].levelData[j][i].y, DrawManager.levelArray[DrawManager.getLevelIndex()].levelData[j][i].img);
                //blockFromSprite.blockType=DrawManager.levelArray[DrawManager.getLevelIndex()].levelData[j][i].blockType;
                if (blockFromSprite!=null&&checkTop(blockFromSprite))
                {
                    return true;
                }
            }
        }
        
        return false;
    }
    
    public boolean checkStanding()
    {
        boolean isStandingOnSomething=false;
        //Approximate which block coords we're in.
        calcBlockCoords();
        
        //System.out.printf("Wally block coords= %d, %d\n", blockX, blockY);
        
        return isStandingOnSomething;
    }
    
    public boolean standingOnBlock() {
        return sideHitting == 'u' && dy != -wallyJump;
    }
    
    public void jump() {
        this.dy = -wallyJump;
    }
}
